package com.dao.impl;

import com.dao.UserDao;
import com.dto.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * Created by hxw on 2017-6-14.
 */
@Repository("userDaoImpl")
public class UserDaoImpl implements UserDao {
    @Resource(name = "sessionFactory")
    SessionFactory sf;
    @Override
    public String register(User user) {
        Session session=sf.openSession();
        Serializable id = session.save(user);
        if(id!=null){
            System.out.println(id);
            return "success";
        }
        return "fail";
    }

    @Override
    public String login(User user) {
        Session session=sf.openSession();
        String sql=" select * from tb_user where username="+user.getUsername()+" and password="+user.getPassword();
        Query query=session.createSQLQuery(sql);
        List<User> list = query.list();
        if(list.size()!=0){
            return "success";
        }else {
        return "fail";}
    }
}
